package com.weavernorth.action.application;


import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CheckLimit implements Action {

    private Logger log = Logger4jUtil.getLoggerByName("/action/application/CheckLimit/");

    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        RecordSet rsDetail = new RecordSet();
        RecordSet rsCheck = new RecordSet();
        RecordSet rsCheckTwo = new RecordSet();
        RecordSet rsCheckThree = new RecordSet();
        RecordSet rsCheckFour = new RecordSet();
        RecordSet rsCheckFive = new RecordSet();
        RecordSet rsCheckSix = new RecordSet();
        RecordSet rsCheckSeven = new RecordSet();
        RecordSet rsCheckEngit = new RecordSet();

        //明细表的月份
        StringBuffer strMonth = new StringBuffer();
        //当前日期的月份
        StringBuffer strNowMonth = new StringBuffer();
        //当前日期的日
        StringBuffer strNowdate = new StringBuffer();

        //获取请求ID
        String strLcId = requestInfo.getRequestid();
        log.info("requestid的值是：" + strLcId);
        //获取表单名称
        //String strTableName = requestInfo.getRequestManager().getBillTableName();
        RecordSet rsTable = new RecordSet();
        String strTableName = "";
        rsTable.execute("select tablename  from workflow_bill where id = (select formid from workflow_base where id=(select workflowid from workflow_requestbase  where requestid='" + strLcId + "') and isbill=1)");
        if (rsTable.next()) {
            strTableName = rsTable.getString("tablename");
        }
        log.info("tablename的值是：" + strTableName);
        //获取表单id（workflowid）
        String strBdId = requestInfo.getWorkflowid();
        log.info("workflowid的值是：" + strBdId);

        //获取对应流程的字段名称
        String strFieldName = bb.getPropValue("checklimint", strBdId);
        //格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前日期
        Date date = new Date();

        String formatDate = sdf.format(date);
        log.info("当前日期：" + formatDate);
        Date parseDate = null;
        try {
            parseDate = sdf.parse(formatDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取月份值
        Calendar calender = Calendar.getInstance();
        calender.setTime(parseDate);
        strNowMonth.append(String.valueOf(calender.get(Calendar.MONTH) + 1));
        log.info("当前日期的月份值：" + strNowMonth.toString());
        strNowdate.append(String.valueOf(calender.get(Calendar.DATE)));
        log.info("当前日期的日值：" + strNowdate.toString());


        //查询明细表数据的sql
        String strSqlDetail = "select * from " + strTableName + "_dt1 d," + strTableName + " s  where d.mainid=s.id  and  s.requestid = '" + strLcId + "'";
        log.info("查询明细表数据的sql：" + strSqlDetail);
        rsDetail.executeQuery(strSqlDetail);

        //判断是否有值
        if (rsDetail.next()) {
            //获取开始日期
            String strKsrq = Util.null2String(rsDetail.getString(strFieldName));
            //转换格式
            Date dateKsrq = null;
            try {
                dateKsrq = sdf.parse(strKsrq);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //获取月份值
            calender.setTime(dateKsrq);
            strMonth.append(String.valueOf(calender.get(Calendar.MONTH) + 1));
            log.info("明细表单的月份值：" + strMonth.toString());
        }
        //当前日期的月份转为int类型
        int intNowMonth = Integer.parseInt(strNowMonth.toString());
        log.info("当前日期月份的int类型：" + intNowMonth);
        //明细表的月份转为int类型
        int intDetailMonth = Integer.parseInt(strMonth.toString());
        log.info("明细表月份的int类型：" + intDetailMonth);
        //当前日期的日转为int类型
        int intNowdate = Integer.parseInt(strNowdate.toString());
        log.info("当前日期的日的int类型：" + intNowdate);
        //判断明细月份是否为12月
        if (intDetailMonth == 12) {
            //次月4日之前都可以进入考勤表
            int intDetailMonthTwo = 1;
            //如果当前月和明细表月份的次月相等或者和明细月份相等
            if (intNowMonth == intDetailMonth) {
                try {
                    log.info("可以进入考勤报表");
                    String strCheckSql = "update " + strTableName + " set  sfxydrbb = '0' where requestid='" + strLcId + "'";
                    log.info("修改是否可以进入报表字段的sql:" + strCheckSql);
                    rsCheck.executeQuery(strCheckSql);
                } catch (Exception ex) {
                    log.info("执行异常1,异常错误：" + ex.toString());
                }
            }
            if (intNowMonth == intDetailMonthTwo) {
                //判断是否在次月四日之前
                if (intNowdate > 4) {
                    try {
                        log.info("不能进入考勤报表");
                        String strCheckSqlTwo = "update " + strTableName + " set  sfxydrbb = '1' where requestid='" + strLcId + "'";
                        log.info("修改是否可以进入报表字段的sql:" + strCheckSqlTwo);
                        rsCheckTwo.executeQuery(strCheckSqlTwo);
                    } catch (Exception ex) {
                        log.info("执行异常2,异常错误：" + ex.toString());
                    }
                } else {
                    try {
                        log.info("可以进入考勤报表");
                        String strCheckSqlThree = "update " + strTableName + " set  sfxydrbb = '0' where requestid='" + strLcId + "'";
                        log.info("修改是否可以进入报表字段的sql:" + strCheckSqlThree);
                        rsCheckThree.executeQuery(strCheckSqlThree);
                    } catch (Exception ex) {
                        log.info("执行异常3,异常错误：" + ex.toString());
                    }
                }
            }
            if (intNowMonth != intDetailMonth && intNowMonth != intDetailMonthTwo) {
                try {
                    log.info("不可以进入考勤报表");
                    String strCheckSqlFour = "update " + strTableName + " set  sfxydrbb = '1' where requestid='" + strLcId + "'";
                    log.info("修改是否可以进入报表字段的sql:" + strCheckSqlFour);
                    rsCheckFour.executeQuery(strCheckSqlFour);
                } catch (Exception ex) {
                    log.info("执行异常4,异常错误：" + ex.toString());
                }
            }

        }
        //明细月份不是12月
        else {
            //如果当前月和明细表月份的次月相等或者和明细月份相等
            if (intNowMonth == intDetailMonth) {
                try {
                    log.info("可以进入考勤报表");
                    String strCheckSqlFive = "update " + strTableName + " set  sfxydrbb = '0' where requestid='" + strLcId + "'";
                    log.info("修改是否可以进入报表字段的sql:" + strCheckSqlFive);
                    rsCheckFive.executeQuery(strCheckSqlFive);
                } catch (Exception ex) {
                    log.info("执行异常5,异常错误：" + ex.toString());
                }
            } else if (intNowMonth == (intDetailMonth + 1)) {
                if (intNowdate > 4) {
                    try {
                        log.info("不能进入考勤报表");
                        String strCheckSqlSix = "update " + strTableName + " set  sfxydrbb = '1' where requestid='" + strLcId + "'";
                        log.info("修改是否可以进入报表字段的sql:" + strCheckSqlSix);
                        rsCheckSix.executeQuery(strCheckSqlSix);
                    } catch (Exception ex) {
                        log.info("执行异常6,异常错误：" + ex.toString());
                    }
                } else {
                    try {
                        log.info("可以进入考勤报表");
                        String strCheckSqlSeven = "update " + strTableName + " set  sfxydrbb = '0' where requestid='" + strLcId + "'";
                        log.info("修改是否可以进入报表字段的sql:" + strCheckSqlSeven);
                        rsCheckSeven.executeQuery(strCheckSqlSeven);
                    } catch (Exception ex) {
                        log.info("执行异常6,异常错误：" + ex.toString());
                    }
                }
            } else if (intNowMonth == (intDetailMonth - 1) || (intNowMonth == 12 && intDetailMonth == 1)) {
                try {
                    String strUpSql = "update " + strTableName + " set sfxydrbb='0' where requestid='" + strLcId + "'";
                    log.info("请下个月的假,进入报表:" + strUpSql);
                    rsCheckSeven.executeQuery(strUpSql);
                } catch (Exception ex) {
                    log.info("执行异常7,异常错误：" + ex.toString());
                }
            } else
            //if (intNowMonth != intDetailMonth && intNowMonth != (intDetailMonth + 1) && intNowMonth != (intDetailMonth - 1))
            {
                try {
                    log.info("不可以进入考勤报表");
                    String strCheckSqlEngit = "update " + strTableName + " set  sfxydrbb = '1' where requestid='" + strLcId + "'";
                    log.info("修改是否可以进入报表字段的sql:" + strCheckSqlEngit);
                    rsCheckEngit.executeQuery(strCheckSqlEngit);
                } catch (Exception ex) {
                    log.info("执行异常8,异常错误：" + ex.toString());
                }
            }
        }

        log.info("执行成功！");
        return SUCCESS;
    }
}
